#include <glib.h>

typedef struct my_data_tag {
    int id;
    char *name;
}my_data_t;


gboolean _foreach_str_struct_func (gpointer key, gpointer value, gpointer data)
{
    g_print("key:%s, id:%d, value:%s user_data:%s \n", \
        (char *)key, ((my_data_t *)value)->id, ((my_data_t *)value)->name, (gchar *)data);
    return 0;
}

void _str_struct_key_destroy_func(gpointer data)
{
    if(NULL != data) {
        g_free(data);
    }
}

void _str_struct_value_destroy_func(gpointer data)
{
    my_data_t *my_data = data;
    if(NULL != my_data) {
        if(NULL != my_data->name) {
            g_free(my_data->name);
        }
        g_free(my_data);
    }
}

gint gtree_test_str_struct_data(void)
{
    GTree *tree = NULL;
    my_data_t *my_data[3];
    gint i = 0;

    tree = g_tree_new_full((GCompareDataFunc)strcmp,NULL,_str_struct_key_destroy_func,_str_struct_value_destroy_func);
    if(NULL == tree) {
        g_print("g_tree_new return NULL \n");
        return 0;
    }

    for(i=0; i<3; i++) {
        my_data[i] = g_new0(my_data_t,1);
        my_data[i]->id = i*10;
        my_data[i]->name = g_strdup_printf("name-%d", i*100);
        g_tree_insert(tree, (gpointer)g_strdup_printf("key-%d", i), (gpointer)my_data[i]);
    }

    g_tree_foreach(tree, _foreach_str_struct_func, "str-struct");

    g_tree_destroy(tree);

    return 0;
}


gint main(gint argc, gchar **argv)
{
    gtree_test_str_struct_data();

    return 0;
}